package com.appbee.cm.client.nav.ui;

import javax.inject.Inject;

import com.appbee.cm.client.nav.presenter.NavItemWrap;
import com.appbee.cm.client.nav.presenter.NavSelectionBoxPresenter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class NavSelectionBox extends Composite implements
		NavSelectionBoxPresenter.View, HasText
{

	private static NavSelectionBoxUiBinder uiBinder = GWT
			.create(NavSelectionBoxUiBinder.class);

	interface NavSelectionBoxUiBinder extends UiBinder<Widget, NavSelectionBox>
	{
	}

	@UiField
	Element label;

	@UiField
	Label value;

	@UiField
	Button selectButton;

	private final NavSelectionBoxPresenter presenter;

	@UiField(provided = true)
	INavSelectStyle style;

	@Inject
	public NavSelectionBox(	NavSelectionBoxPresenter navPresenter,
							INavSelectStyle navSelectStyle)
	{
		this.presenter = navPresenter;
		this.style = navSelectStyle;
		presenter.setView(this);
		initWidget(uiBinder.createAndBindUi(this));
		selectButton.addClickHandler(new ClickHandler()
		{
			
			@Override
			public void onClick(ClickEvent event)
			{
				presenter.onSelectClick(value);
			}
		});
	}

	@Override
	public void setValue(NavItemWrap navItemValue)
	{
		value.setText(navItemValue == null ? "" : navItemValue.getName());
	}

	public NavItemWrap getValue()
	{
		return presenter.getValue();
	}

	public void setNavId(Long parentLongId)
	{
		presenter.setNavIdValue(parentLongId);
	}

	@Override
	public String getText()
	{
		return label.getInnerText();
	}

	@Override
	public void setText(String text)
	{
		label.setInnerText(text);
	}

}
